/**	 
 * @Description 
 * @Path		com.bbx.api.sdk.SDK.java 
 * @Author		年向   
 * @Date		2015年7月14日 下午5:01:12 
 * @Company	 	蓝海（福建）信息技术有限公司	 
 */
package com.bbx.api.sdk;

import android.R.integer;
import android.content.Context;
import android.content.IntentFilter;
import android.os.Environment;

import java.io.File;
import java.io.IOException;

import com.bbx.api.gclient.GMsgBroadcastReceiver;
import com.bbx.api.gclient.GMsgListener;
import com.bbx.api.gclient.GMsgPublisher;
import com.bbx.api.gclient.Observer;
import com.gim.client;

/**
 * @Description
 * @Path com.bbx.api.sdk.SDK.java
 * @Author 年向
 * @Date 2015年7月14日 下午5:01:12
 * @Company 蓝海（福建）信息技术有限公司
 */
public class SDK  {

	public GMsgPublisher publisher;
	private GMsgListener listener;
	public client mClient = null;
	public static String VER = "android";
	public String serviceLogPath = null;
	public static SDK ins;

	public static SDK GetSDK() {
		if (ins == null) {
			ins = new SDK();
		}
		return ins;
	}

	public void init(Context context) {
		// 实例化推送类
		publisher = new GMsgPublisher();
		// 注册广播
		IntentFilter filter = new IntentFilter();
		filter.addAction(GMsgBroadcastReceiver.TAG);
		filter.setPriority(Integer.MAX_VALUE);
		context.registerReceiver(new GMsgBroadcastReceiver(), filter);
		// 监听
		listener = new GMsgListener(context);
		// 实例化客户端
		mClient = new client();
		mClient.init(listener);
		String SDCARD_PATH = Environment.getExternalStorageDirectory()
				.toString();
		serviceLogPath = SDCARD_PATH + "/bbxpc/clientsdklog.log";
		File f = new File(serviceLogPath);
		if(!f.exists()){
			  try {
				f.createNewFile();
			} catch (IOException e) {
				e.printStackTrace();
			}    
		}
		mClient.setLogpath(serviceLogPath);

	}

	/**
	 * 网络加密
	 */
	public static String encrypt(String str) {
		return client.encrypt(str);
	}

	/**
	 * 网络解密
	 */
	public static String decrypt(String str) {
		return client.decrypt(str);
	}

	/**
	 * 本地解密
	 */
	public static String aesDecrypt(String str) {
		return client.aesDecrypt(str);
	}

	/**
	 * 本地加密
	 */
	public static String aesEncrypt(String str) {
		return client.aesEncrypt(str);
	}

	
	
	
	public void addObs(Observer observer) {
		if (publisher != null) {
			publisher.addOb(observer);
		}
	}

	public void removeObs(Observer observer) {
		if (publisher != null) {
			publisher.removeOb(observer);
		}
	}

	public void clearObs() {
		if (publisher != null) {
			publisher.clear();
		}
	}

	public void login(String uid, String token) {
		if (mClient != null) {
			mClient.login(Config.getInstance().IP, Config.getInstance().PORT, VER, 1, uid, Config.getInstance().DEFAULT, token);
			mClient.loglevel(1);
		}
	}
	public void disconnect(String uid){
		if (mClient != null) {
			mClient.disconnect(uid);
		}
	}
	
	public void request(String uid,String sn,int serType,String json){
		if(mClient != null){
			mClient.request(uid, sn, serType, json);
		}
	}
	
	/***
	 * 
	* @Title:       keepAlive
	* @param        @param uid
	* @param        @param sn
	* @param        @param serType
	* @param        @return      
	* @return       int    返回类型
	* @throws
	* @author       lulinhua
	* @date         2016-9-2 上午9:08:33
	* @Description: TODO 链路维护
	 */
	public int keepAlive(String uid,String sn,int serType) {
		if(mClient != null){
			return mClient.keepAlive(uid, sn, serType);
		} 
		return -1;
	}
	
	/**
	 * 
	* @Title:       getServiceLogPath
	* @param              
	* @return       void    返回类型
	* @throws
	* @author       lulinhua
	* @date         2016-5-12 下午2:15:38
	* @Description: TODO 获取服务器后台写的日志路径
	 */
	public String getServiceLogPath() {
		return serviceLogPath;
	}

	public void setServiceLogPath(String serviceLogPath) {
		this.serviceLogPath = serviceLogPath;
		mClient.setLogpath(serviceLogPath);
	}
	
}
